﻿@model Sealight.Core.DataAdapter.topic_localized
@{
    ViewBag.Title = "Thêm sửa chủ đề";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";

    string title = string.Empty, description = string.Empty, detail = string.Empty, id = string.Empty;
    
    if(Model!= null)
    {
        title = Model.title;
        description = Model.description;
        detail = Model.body;
        id = Model.id.ToString();   
    }
    
}
@section _Head{
    <script type="text/javascript">
        $("#nav-left").accordion("activate", 4);
    </script>
 }
<h2>Thêm sửa chủ đề</h2>
<script type="text/javascript">
    $(document).ready(function () {
        //<![CDATA[
        // Replace the <textarea id="editor1"> with an CKEditor instance.
        var description_editor, detail_editor;

        if (description_editor)
            description_editor.destroy();
        if (detail_editor)
            detail_editor.destroy();

        description_editor = CKEDITOR.replace('description_field');
        detail_editor = CKEDITOR.replace('detail_field', {
            filebrowserImageBrowseUrl: '/file/Index',
            filebrowserImageUploadUrl: '/file/UploadImageWithCKEditor?folder=editors',
            height: 500,
            filebrowserWindowWidth: '640',
            filebrowserWindowHeight: '480'

        });
        // The instanceReady event is fired when an instance of CKEditor has finished
        // its initialization.
        CKEDITOR.on('instanceReady', function (ev) {
            // Show the editor name and description in the browser status bar.

        });
        //]]>
    });
</script>
<script type="text/javascript">
    
    $(document).ready(function () {

        $("a.buttons").button();

        $('#btn_save').click(function () {
            $('#new_edit_form').trigger("submit");
        });

        $('#btn_cancel').click(function () {
            window.location = "/admin/topic/index";
        });

        var container = $('div.container_error');

        //FORM VALIDATE & SUBMIT              
        //form validate & submit
        $("#new_edit_form").validate({
            rules: {
                title_field: {// compound rule 
                    required: true
                }
            },
            messages: {
                title_field: {
                    required: "Tiêu đề bắt buộc nhập."
                }
            },
            errorElement: "li",
            errorContainer: container,
            errorLabelContainer: $(".error", container)
            ,
            submitHandler: function (form) {
                var id = $('#hidden_id').val();
                var title = $('#title_field').val();

                var oEditorDescription = CKEDITOR.instances.description_field;
                var oEditorDetail = CKEDITOR.instances.detail_field;

                var description = oEditorDescription.getData();
                var detail = oEditorDetail.getData();

                JsUtils.ShowLoader('#manage_container', 'Lưu...');           
                $.ajax({
                    type: "POST",
                    url: "/Admin/Topic/AddOrUpdate",
                    data: ({id: id, title: title, description: HtmlEncode(description),
                        detail: HtmlEncode(detail)
                    }),
                    dataType: "json",
                    success: function (d) {
                        if (d != null) {

                            if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                if (d.ErrorFriendlyMessage != null) {
                                    ShowErrorMessage(d.ErrorFriendlyMessage);
                                }
                                return;
                            }
                            else {
                                window.location = "/admin/topic/index";
                            }
                        }
                    },
                    complete: function (d) {
                        JsUtils.hideLoader('#manage_container');
                    },
                    cache: false
                }); //end .ajax call
            }

        }); //end form.validate  
    });   

</script>
<div id="manage_container">
    <div id="new_edit_container">
        <form id="new_edit_form" method="post" action="" enctype="multipart/form-data">
        <fieldset style="width: 700px;" class="Internal">
            <legend class="Internal"></legend>
            <div class="container_error">
                <h5>
                    Vui lòng kiểm tra lại các lỗi...</h5>
                <ol class="error">
                </ol>
            </div>          
            <div id="infoColumn" style="width: 650px;">
                <table class="Internal">
                    <tbody>
                        <tr>
                            <td style="padding-top: 10px;">
                                <strong class="Internal">Tiên đề:</strong>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type="text" id="title_field" name="title_field" style="width: 340px" class="Internal" value="@title"/>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table class="Internal">
                    <tbody>
                        <tr>
                            <td style="padding-top: 10px;">
                                <strong class="Internal">Mô tả:</strong>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <textarea id="description_field" name="description_field" style="width: 340px" rows="20"
                                    class="Internal">@description</textarea>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table class="Internal">
                    <tbody>
                        <tr>
                            <td style="padding-top: 10px;">
                                <strong class="Internal">Chi tiết:</strong>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <textarea id="detail_field" name="detail_field" style="width: 340px" rows="50" class="Internal">@detail</textarea>
                            </td>
                        </tr>
                    </tbody>
                </table>             
            </div>
            <br clear="all" />
            <br />
            <div style="width: 600px; text-align: right; margin-bottom: 10px; float: left;">
                <input type="hidden" id="hidden_id" value="@id" />
                <a href="javascript:void(0);" class="buttons" id="btn_save">Lưu </a><a href="javascript:void(0);"
                    class="buttons" id="btn_cancel">Hủy </a>
            </div>
        </fieldset>
        </form>
    </div>  
</div>

